package com.example.arraylist;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.AutoText;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.InputStream;
import java.util.StringTokenizer;

public class MainActivity extends AppCompatActivity {
    String input="";
    CircularArrayQueue<Integer> circularArrayQueue=new CircularArrayQueue();
    EditText editText;
    TextView output;
    EditText editText2,editText3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void Edit(View view) {
        int editpoint=Integer.parseInt(editText3.getText().toString());
        int editelement=Integer.parseInt(editText2.getText().toString());
        circularArrayQueue.change((editpoint-1),editelement);
        output.setText(circularArrayQueue.toString());
    }

    public void Delete(View view) {
        circularArrayQueue.dequeue();
        output.setText(circularArrayQueue.toString());
    }

    public void Add(View view) {
        input=editText.getText().toString();

        StringTokenizer stn2=new StringTokenizer(input," ,");
        while (stn2.hasMoreElements()){
            circularArrayQueue.enqueue(Integer.parseInt(stn2.nextToken()));
        }
        output.setText(circularArrayQueue.toString());
    }

    public void Input(View view) {
        editText=(EditText)findViewById(R.id.editText);
        output=(TextView)findViewById(R.id.textView3);
        editText2=(EditText)findViewById(R.id.input2);
        editText3=(EditText)findViewById(R.id.input3);

        //读取输入文本
        input=editText.getText().toString();

        //将输入的文本转化为数组队列
        StringTokenizer stn=new StringTokenizer(input," ,");
        int number;
        while (stn.hasMoreElements()){
            number= Integer.parseInt(stn.nextToken());
            circularArrayQueue.enqueue(number);
        }

        //放到textView中输出
        output.setText(circularArrayQueue.toString());

        //清空editText
        editText.setText("");
    }

    public void reverse(View view) {
        CircularArrayQueue<Integer> temp=new CircularArrayQueue<>();
        int length=circularArrayQueue.length();
        for (int i=0;i<length;i++){
            if (circularArrayQueue.get(length-1-i)!=null){
                temp.enqueue(circularArrayQueue.get(length-1-i));
            }
        }
        circularArrayQueue=temp;
        output.setText(circularArrayQueue.toString());
    }
}
